package com.happy016.controller;

import com.happy016.dao.UserRepository;
import com.happy016.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/get/{id}")
    public User getUser(@PathVariable Integer id){
       User user =  userRepository.findOne(id);
       return user;
    }

    @GetMapping("/get")
    public User getUserAndUsername(Integer id,String username){
        User user = new User();
        user.setUsername(username);
        user.setId(id);
        Example<User> example = Example.of(user);
        User ret =  userRepository.findOne(example);
        return ret;
    }

    @GetMapping("/get/map")
    public User getUserByMap(Integer id,String username){
        return this.getUserAndUsername(id,username);
    }


    @GetMapping("/post")
    public User getUserByMap(User user){
        Example<User> example = Example.of(user);
        User ret =  userRepository.findOne(example);
        return ret;
    }
}
